﻿//This file is part of Search Engine by Tomasz Kusienicki.
// 
//This Search Engine is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//any later version.
// 
//Foobar is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
// 
//You should have received a copy of the GNU General Public License
//on <http://www.gnu.org/licenses/>.

using System.Data.SqlClient;
using AdminApp1.Common;
using AdminApp1.DataSource;
using AdminApp1.Models;
using AdminApp1.Views;

namespace AdminApp1.Presenters
{ 
    public class MainPresenter : PresenterBase<MainView, MainPresenter, MainModel>
    {

        #region Constructor
        public MainPresenter(MainView view, MainModel model)
            : base(view, model)
        {
            Model.ApplicationName = @"AdminApp 1.0";
            Model.MsgConnecting = @"connecting...";
            Model.MsgConnectionFail = @"connection fail";

            Model.Connection = new Connection();
            Model.DataControlModel = new DataControlModel();

            ShowMsg("login");
            LoadSettings(); 
        }
        #endregion


        #region Load and Save Settings
        private void LoadSettings()
        {
            Model.RememberMe = Settings.Default.RememberMe;
            var connectionString = new SqlConnectionStringBuilder(Settings.Default.Connection);
            Model.Server = connectionString.DataSource;
            Model.Database = connectionString.InitialCatalog;
            Model.User = connectionString.UserID;
            Model.Password = connectionString.Password;
            Model.TrustedConnection = connectionString.IntegratedSecurity;
        }

        private void SaveSettings()
        {
            Settings.Default.RememberMe = Model.RememberMe;
            Settings.Default.Connection = CreateConnectionString();
            Settings.Default.Save();

            Properties.Settings.Default.SearchEngineDatabaseConnectionString = CreateConnectionString();
            Properties.Settings.Default.Save();
        }
        #endregion

        #region Create ConnectionString
        private string CreateConnectionString()
        {
            var connectionStringBuilder = new SqlConnectionStringBuilder
            {
                DataSource = Model.Server,
                InitialCatalog = Model.Database,
                UserID = Model.User,
                Password = Model.Password,
                IntegratedSecurity = Model.TrustedConnection
            };
            return connectionStringBuilder.ConnectionString;
        }
        #endregion

        #region Show Message
        private void ShowMsg(string msg)
        {
            View.Text = string.Format("{0} - {1}", Model.ApplicationName, msg);
        }
        #endregion


        #region Handle: ShowDataControlView, PropertiesChanged, Disconnect
        public void HandleShowDataControlView()
        {
            ShowMsg(Model.MsgConnecting);
            Model.Connection.ConnectionString = CreateConnectionString();
            Model.Connection.Connect();

            if(!Model.RememberMe)
            {
                Model.User = string.Empty;
                Model.Password = string.Empty;
            }
            SaveSettings();

            if (!Model.Connection.IsConnected)
            {
                ShowMsg(Model.MsgConnectionFail);
            }
            else
            {
                if (Model.DataControlPresenter == null)
                {
                    Model.DataControlPresenter = new DataControlPresenter(new DataControlView(), Model.DataControlModel, this);
                    Model.DataControlPresenter.Initialize();
                    Model.DataControlPresenter.HandleConnected();
                }
                else
                {
                    Model.DataControlPresenter.HandleConnected();
                }
                View.Hide();
            }
        }

        public void HandlePropertiesChanged()
        {
            Model.TrustedConnection = View.cbTrusted.Checked;
            SaveSettings();
        }

        public void HandleDisconnect()
        {
            Model.Connection.Disconnect();
            ShowMsg("login");
        }
        #endregion

    }
}
